/***************************************************************************//**
 * @file CGI/AirportTaxiways.cpp
 * @author  Marek M. Cel <marekcel@mscsim.org>
 *
 * @section LICENSE
 *
 * Copyright (C) 2014 Marek M. Cel
 *
 * This file is part of MScSim. You can redistribute and modify it under
 * the terms of GNU General Public License as published by the Free Software
 * Foundation; either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.
 * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Further information about the GNU General Public License can also be found
 * on the world wide web at http://www.gnu.org.
 ******************************************************************************/
#ifndef AIRPORT_TAXIWAYS_CPP
#define AIRPORT_TAXIWAYS_CPP
#endif

////////////////////////////////////////////////////////////////////////////////

#include <iostream>

#include <osg/Geode>
#include <osg/Geometry>
#include <osg/Material>
#include <osg/PositionAttitudeTransform>
#include <osg/ShapeDrawable>
#include <osg/Texture2D>

#include <osgDB/ReadFile>

#include <CGI/Airport.h>
#include <CGI/AirportTaxiways.h>

////////////////////////////////////////////////////////////////////////////////

using namespace std;

////////////////////////////////////////////////////////////////////////////////

AirportTaxiways::AirportTaxiways( CGI_Data *data ) :
    m_data ( data )
{
    m_airportTaxiwaysGroup = new osg::Group();
    m_airportTaxiwaysGroup->setName( "AirportTaxiwaysGroup" );

    createTaxiways();
}

////////////////////////////////////////////////////////////////////////////////

void AirportTaxiways::createTaxiways()
{
    double y_cntr = 0.0;
    double y_edge = 0.0;
    double y_unto = 0.0;

    int iter = 0;

    y_unto = fabs( Airport::m_taxiDistance + 1.0e-4 );

    y_edge = y_cntr + Airport::m_halfWidth;

    while ( fabs( y_edge ) < y_unto )
    {
        y_cntr = y_cntr + 2.0 * Airport::m_halfWidth;
        y_edge = y_cntr + Airport::m_halfWidth;

        // direction
        Airport::createSegment( m_airportTaxiwaysGroup,
                                    -1850.0, y_cntr - Airport::m_halfWidth, -2000.0, y_cntr + Airport::m_halfWidth,
                                    "data/textures/terrain/ground_1.png", true );

        if ( iter == 0 )
            Airport::createSegment( m_airportTaxiwaysGroup, -1850.0, -1800.0, y_cntr, "data/textures/runway/taxi_runway_hold_lat_r.png" );
        else
            Airport::createSegment( m_airportTaxiwaysGroup, -1850.0, -1800.0, y_cntr, "data/textures/runway/taxi_centerline_lat.png" );

        Airport::createSegment( m_airportTaxiwaysGroup,
                                    -800.0, y_cntr - Airport::m_halfWidth, -1800.0, y_cntr + Airport::m_halfWidth,
                                    "data/textures/terrain/ground_1.png", true );

        if ( iter == 0 )
            Airport::createSegment( m_airportTaxiwaysGroup, -800.0, -750.0, y_cntr, "data/textures/runway/taxi_runway_hold_lat_r.png" );
        else
            Airport::createSegment( m_airportTaxiwaysGroup, -800.0, -750.0, y_cntr, "data/textures/runway/taxi_centerline_lat.png" );

        Airport::createSegment( m_airportTaxiwaysGroup,
                                    0.0, y_cntr - Airport::m_halfWidth, -750.0, y_cntr + Airport::m_halfWidth,
                                    "data/textures/terrain/ground_1.png", true );

        // direction
        Airport::createSegment( m_airportTaxiwaysGroup,
                                    1850.0, y_cntr + Airport::m_halfWidth, 2000.0, y_cntr - Airport::m_halfWidth,
                                    "data/textures/terrain/ground_1.png", true );

        if ( iter == 0 )
            Airport::createSegment( m_airportTaxiwaysGroup, 1850.0, 1800.0, y_cntr, "data/textures/runway/taxi_runway_hold_lat_l.png" );
        else
            Airport::createSegment( m_airportTaxiwaysGroup, 1850.0, 1800.0, y_cntr, "data/textures/runway/taxi_centerline_lat.png" );

        Airport::createSegment( m_airportTaxiwaysGroup,
                                    800.0, y_cntr + Airport::m_halfWidth, 1800.0, y_cntr - Airport::m_halfWidth,
                                    "data/textures/terrain/ground_1.png", true );

        if ( iter == 0 )
            Airport::createSegment( m_airportTaxiwaysGroup, 800.0, 750.0, y_cntr, "data/textures/runway/taxi_runway_hold_lat_l.png" );
        else
            Airport::createSegment( m_airportTaxiwaysGroup, 800.0, 750.0, y_cntr, "data/textures/runway/taxi_centerline_lat.png" );

        Airport::createSegment( m_airportTaxiwaysGroup,
                                    0.0, y_cntr + Airport::m_halfWidth, 750.0, y_cntr - Airport::m_halfWidth,
                                    "data/textures/terrain/ground_1.png", true );

        iter++;
    }

    y_cntr = y_cntr + 2.0 * Airport::m_halfWidth;

    // direction
    Airport::createSegment( m_airportTaxiwaysGroup,
                                -1850.0, y_cntr - Airport::m_halfWidth, -2000.0, y_cntr + Airport::m_halfWidth,
                                "data/textures/terrain/ground_1.png", true );

    Airport::createSegment( m_airportTaxiwaysGroup, -1850.0, -1800.0, y_cntr, "data/textures/runway/taxi_centerline_bl.png" );

    Airport::createSegment( m_airportTaxiwaysGroup,
                                -800.0, y_cntr - Airport::m_halfWidth, -1800.0, y_cntr + Airport::m_halfWidth,
                                "data/textures/runway/taxi_centerline.png", true );

    Airport::createSegment( m_airportTaxiwaysGroup, -800.0, -750.0, y_cntr, "data/textures/runway/taxi_crossing_l.png" );

    Airport::createSegment( m_airportTaxiwaysGroup,
                                0.0, y_cntr - Airport::m_halfWidth, -750.0, y_cntr + Airport::m_halfWidth,
                                "data/textures/runway/taxi_centerline_open_r.png", true );

    // direction
    Airport::createSegment( m_airportTaxiwaysGroup,
                                1850.0, y_cntr + Airport::m_halfWidth, 2000.0, y_cntr - Airport::m_halfWidth,
                                "data/textures/terrain/ground_1.png", true );

    Airport::createSegment( m_airportTaxiwaysGroup, 1850.0, 1800.0, y_cntr, "data/textures/runway/taxi_centerline_br.png" );

    Airport::createSegment( m_airportTaxiwaysGroup,
                                800.0, y_cntr + Airport::m_halfWidth, 1800.0, y_cntr - Airport::m_halfWidth,
                                "data/textures/runway/taxi_centerline.png", true );

    Airport::createSegment( m_airportTaxiwaysGroup, 800.0, 750.0, y_cntr, "data/textures/runway/taxi_crossing_r.png" );

    Airport::createSegment( m_airportTaxiwaysGroup,
                                0.0, y_cntr + Airport::m_halfWidth, 750.0, y_cntr - Airport::m_halfWidth,
                                "data/textures/runway/taxi_centerline_open_l.png", true );
}
